package com.day20180916;
import java.io.IOException;
import java.util.Properties;

public class WeaponFactory {

    private static WeaponFactory instance = new WeaponFactory();
    private WeaponFactory(){}
    public static WeaponFactory getInstance() {
        return instance;
    }

    public Weapon getWeapon(String classID){
        Properties prop = new Properties();
        try {
            System.out.println("read began");
            prop.load(WeaponFactory.class.getResourceAsStream("src\\main\\resources\\weapon.properties"));
        } catch (IOException e) {
            System.out.println("读取配置文件Weapon.properties失败");
            e.printStackTrace();
        }

        //从配置文件中获取类全名
        String className = prop.getProperty(classID);
        Weapon weapon = null;
        try {
            Class weaponClass = Class.forName(className);
            weapon = (Weapon) weaponClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return weapon;
    }
}
